package io.springboot.jpa.examples.springbootjpaexamples.model;

import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author yzc
 * @description
 * @date 2018/9/7
 */
@Entity
@Table(name = "role")
public class Role extends BaseBean implements Serializable {
    private static final long serialVersionUID = -4827827923403200949L;
    private String name;
    private String description;
    private Set<User> userSet = new HashSet<>();
    private List<Menu> menuList = new ArrayList<>();

    @Column(name = "name", length = 10, unique = true)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "description", length = 30)
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @JsonIgnore // ManyToMany的关系中要在其中一方加入@JsonIgnore，否则Spring会转换对象死循环，HttpMessageNotWritableException
    @ManyToMany
    @JoinTable(name = "user_role", // 中间表的表名
            joinColumns = @JoinColumn(name = "role_id"), // 本表的主键
            inverseJoinColumns = @JoinColumn(name = "user_id") // 被关联表的主键
    )
    public Set<User> getUserSet() {
        return userSet;
    }

    public void setUserSet(Set<User> userSet) {
        this.userSet = userSet;
    }

    @ManyToMany
    @JoinTable(name = "role_menu", joinColumns = {@JoinColumn(name = "role_id")}, inverseJoinColumns = {@JoinColumn(name = "menu_id")})
    public List<Menu> getMenuList() {
        return menuList;
    }

    public void setMenuList(List<Menu> menuList) {
        this.menuList = menuList;
    }
}
